// pages/coupon/coupon.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isactive: 1,
    isactive2: 1,
    isempty: false,
    couponstate: 1,
    orderamount: '',
    usetype: 2,
    atindex: -1,
    goodsId: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if (options == undefined) {
      // this.setData({
      //   // usetype: 1,
      //   // couponstate: 2,
      //   // isactive2: 2,
      //   // goodsId:goodsId
      // })
      // this.getMyCoupon(this.data.couponstate, this.data.orderamou)
      return
    }
    if (options.goodsId) {
      var goodsId = JSON.parse(options.goodsId)
      if (Array.isArray(goodsId)) {
        goodsId = goodsId.join(",");
      }
    }

    if (options.use == 1) {
      if (options.iscoupon == 2) {
        this.setData({
          usetype: 1,
          couponstate: 1,
          isactive2: 1,
          goodsId: goodsId,
          orderamount: options.orderamoun
        })
        // this.orderamou=options.orderamoun

        this.getMyCoupon(this.data.couponstate)
      } else {
        this.setData({
          usetype: 1,
          couponstate: 2,
          isactive2: 2,
          goodsId: goodsId,
          orderamount: options.orderamoun
        })
        // this.=
        this.getMyCoupon(this.data.couponstate, this.data.orderamount)
      }

    } else {
      this.setData({
        usetype: 2
      })
      console.log(this.data.usetype)
      this.getMyCoupon(1, '')
    }

  },
  onchickruth(e) {

    if (this.data.atindex == e.currentTarget.dataset.id) {
      this.setData({
        atindex: -1
      })
    } else {
      this.setData({
        atindex: e.currentTarget.dataset.id
      })
    }

  },
  nouse() {
    wx.showToast({
      title: '您无法使用该券',
      icon: 'none'
    })
  },

  //使用优惠券
  usecoupon(e) {
    if (this.data.usetype == 1) {

      console.log(e)




      let pages = getCurrentPages();//当前页面    （pages就是获取的当前页面的JS里面所有pages的信息）

              let prevPage = pages[pages.length - 2];//上一页面（prevPage 就是获取的上一个页面的JS里面所有pages的信息）

               prevPage.setData({
                    couponId:e.currentTarget.dataset.id,
                    couponAmount:e.currentTarget.dataset.couponamount,
                    iscoupon:1,
              })
      // let pages = getCurrentPages();
      // let prevPage = pages[pages.length - 2]; //prevPage 是获取上一个页面的js里面的pages的所有信息。 -2 是上一个页面，-3是上上个页面以此类推。
      wx.navigateBack({ //返回
        delta: 1,
        success: function (e) {
          // prevPage.getCoupon()
          // var page = getCurrentPages().pop();
          // if (page == undefined || page == null) return;
          // page.onLoad();
        }
      })


    } else {
      wx.switchTab({
        url: '/pages/index/index',
        success: function (e) {
          var page = getCurrentPages().pop();
          if (page == undefined || page == null) return;
          page.onLoad();
        }
      })

    }

  },




  couponactive(e) {
    switch (e.currentTarget.dataset.type) {
      case "1":
        if (this.data.isactive2 != e.currentTarget.dataset.type) {
          this.setData({
            atindex: -1,
            couponLists: '',
            isactive2: e.currentTarget.dataset.type,
            couponstate: 1,

          })
          this.getMyCoupon(this.data.couponstate)
        }

        break;
      case "2":
        if (this.data.isactive2 != e.currentTarget.dataset.type) {
          if (this.data.usetype == 1) {
            // console.log('orderamou',this.orderamou)
            this.setData({
              atindex: -1,
              couponLists: '',
              isactive2: e.currentTarget.dataset.type,
              couponstate: 2,
            })
            this.getMyCoupon(this.data.couponstate, this.data.orderamount)

          } else {
            this.setData({
              atindex: -1,
              couponLists: '',
              isactive2: e.currentTarget.dataset.type,
              couponstate: 2,
            })
            this.getMyCoupon(this.data.couponstate)
          }


        }

        break;
      case "3":
        if (this.data.isactive2 != e.currentTarget.dataset.type) {
          this.setData({
            atindex: -1,
            couponLists: '',
            isactive2: e.currentTarget.dataset.type,
            couponstate: 3,
          })
          this.getMyCoupon(this.data.couponstate)
        }
        break;
      case "4":
        if (this.data.isactive2 != e.currentTarget.dataset.type) {
          this.setData({
            atindex: -1,
            couponLists: '',
            isactive2: e.currentTarget.dataset.type,
            couponstate: 3,
          })
          this.getMyCoupon(this.data.couponstate,)
        }

        break;

      default:
        break;
    }
  },
  gomy() {
    wx.switchTab({

      url: '/pages/my/my',
    })
  },
  receivecoupon(e) {
    var t=this
    wx.navigateTo({
      url: '/pages/member/member?couponId=' + e.currentTarget.dataset.id+"&usetype="+t.data.usetype,
    })
    // wx.request({
    //   url: app.globalData.Interfaceurl + 'receiveCoupon',
    //   method: "post",
    //   header: {
    //       "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
    //   },
    //   data: {
    //       userId: app.globalData.userId,
    //       couponId:e.currentTarget.dataset.id
    //   },
    //   success: res => {
    //       console.log(res)
    //       if(res.data.result==0){
    //         // var couponslist=[]
    //         // t.data.couponlist.forEach((element,index) => {
    //         //   if(e.currentTarget.dataset.id==element.id){
    //         //     element.receiveFlag=0
    //         //   }
    //         //   couponslist[index]=element
    //         // });
    //         // t.setData({
    //         //   couponlist:couponslist
    //         // })
    //         this.getMyCoupon(this.data.couponstate,this.data.orderamount)

    //         wx.showToast({
    //           title: '领取成功',
    //           icon:"none"
    //         })



    //       }


    //   }
    // })

  },

  getMyCoupon(couponstate = '', orderamount = '') {
    wx.showLoading({
      title: '请稍等',
      mask:true
    })
    var t = this
    wx.request({
      url: app.globalData.Interfaceurl + 'getMyCoupon',
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
      },
      data: {
        userId: app.globalData.userId,
        couponState: couponstate,
        orderAmount: orderamount,
        goodIds: t.data.goodsId
      },
      success: res => {
        console.log(res)
        if (res.data.result == 0) {
          wx.hideLoading()
          this.setData({
            couponLists: res.data.couponList
          })
          if (res.data.couponList.length > 0) {

            this.setData({
              isempty: false
            })
          } else {
            this.setData({
              isempty: true
            })
          }


        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.message,
            icon: 'none'
          })

        }

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})